package com.weilive.core.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class BearStringUtils {

	private static final char hexDigits[] = {'a','s','d','q','w','e','f','c','v','h','j','k','l','u','m','i'};

	/**
	 * 解密
	 */
	public static byte[] decrypt(String key) throws Exception {
		char[] chars = key.toLowerCase().toCharArray();
		int j = chars.length/2;
		byte[] ret = new byte[j];
		int k = 0;
		for (int i = 0; i < j; i ++) {
			byte b1 = chars[k] < 65 ? (byte)(chars[k] - 48) : (byte)(chars[k] - 87);
			k++;
			byte b2 = chars[k] < 65 ? (byte)(chars[k] - 48) : (byte)(chars[k] - 87);
			k++;
			ret[i] = (byte)((b1 << 4) | b2);
		}
		return ret;
	}

	/**
	 * 加密
	 */
	public static String encrypt(byte[] key) throws Exception {
		int j = key.length;
		char str[] = new char[j * 2];
		int k = 0;
		for (int i = 0; i < j; i++) {
			byte byte0 = key[i];
			str[k++] = hexDigits[byte0 >>> 4 & 0xf];
			str[k++] = hexDigits[byte0 & 0xf];
		}
		return new String(str);
	}

	private static final char codeDigits[] = {
		'a', 'b', 'c', 'D', 
		'E', 'F', 'G', 'h', 
		'i', 'j', 'k', 'L', 
		'm', 'n',      'p', 
		'Q', 'R', 's', 't', 
		'u', 'v', 'W', 'X', 
		'Y', 'Z',  
		'2', '3',      '5',
		'6', '7', '8', '9'
	};

	private static final AtomicInteger code = new AtomicInteger((int)(System.currentTimeMillis()));

	public static String getCode(int prefix) {
		char str[] = new char[8];

		long id = code.incrementAndGet();
		id <<= 8;
		id += prefix & 0xf;
		for (int i = 0; i < 4; i++) {
			str[i] = codeDigits[(int) (id & 0x0000001f)];
			id >>= 5;
			str[7-i] = codeDigits[(int) (id & 0x0000001f)];
			id >>= 5;
		}
		return new String(str);
	}
	
	public static void main(String [] args) {
		for (int i = 0; i < 5; i ++) {
			System.out.println(getCode(140110));
			System.out.println(getCode(140111));
			System.out.println(getCode(140112));
			System.out.println(getCode(140113));
			System.out.println(getCode(140114));
			System.out.println(getCode(140115));
			System.out.println("");
		}
	}
}
